package com.wind.base.api;

import android.util.Log;

import com.wind.base.C;
import com.wind.base.bean.CropBitmap;
import com.wind.base.bean.UploadFile;
import com.wind.base.response.UploadFileResponse;
import com.wind.base.utils.JsonParser;
import com.wind.base.utils.LogUtil;
import com.wind.base.utils.PictureUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.io.IOException;

import okhttp3.Call;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by wind on 2017/3/1.
 */

public class UploadApi {

    public Observable<UploadFileResponse> upload(final UploadFile uploadFile) {
        String path=uploadFile.getPath();
        if (!uploadFile.isDontCompress()) {
            try {
                CropBitmap cropBitmap = PictureUtil.bitmapToPath(uploadFile.getPath());
                path = cropBitmap.getTempPath();
            } catch (IOException e) {
                e.printStackTrace();

            }
        }

        final File file=new File(path);
        return Observable.create(new Observable.OnSubscribe<UploadFileResponse>() {
            @Override
            public void call(final Subscriber<? super UploadFileResponse> subscriber) {
                try {
                    OkHttpUtils.post()
                            .url(C.Api.IMAGE_SERVER_URL)
                            .addFile("img", file.getName(), file)
                            .addHeader("channel","ONQx4Tk9V4zs73uTpqLgSTroBWVzvqpdAkUpuZTE")
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {
                                    uploadFile.setUploadState(UploadFile.STATE_UPLOAD_ERROR);
                                    subscriber.onError(e);
                                }

                                @Override
                                public void onResponse(String response, int id) {
                                    try {
                                        UploadFileResponse resp = JsonParser.parserObject(response, UploadFileResponse.class);
                                        uploadFile.setUploadState(UploadFile.STATE_UPLOAD_SUCCESS);
                                        uploadFile.setUploadedUrl(resp.getUploadedUrls().get(0));
                                        LogUtil.e(uploadFile.getUploadedUrl());
                                        resp.setUploadFile(uploadFile);
                                        subscriber.onNext(resp);
                                        subscriber.onCompleted();
                                    } catch (Exception e) {

                                        e.printStackTrace();
                                    }
                                }

                                @Override
                                public void inProgress(float progress, long total, int id) {
                                    uploadFile.setUploadState(UploadFile.STATE_UPLOADING);
                                    UploadFileResponse resp=new UploadFileResponse();
                                    resp.setUploadFile(uploadFile);
                                    //subscriber.onNext(resp);
                                    Log.e("Upload","progress:"+progress);
                                }
                            });
                }catch (Exception e){
                    e.printStackTrace();
                    subscriber.onError(e);
                }
            }
        });


    }
}
